﻿/*******************************************************************  
 * Copyright (C) vancl.com
 * All rights reserved. 
 * 
 * Created By: VANCLOA\hanxianlong
 * Create Date:2011/8/17 11:49:25
 * Last Modified By:VANCLOA\hanxianlong
 * Last Modified Date:2011/8/17 11:49:25
 * Description: 类型转换类,负责从pdm中的类型转换为c#类型
 * ********************************************************************/
#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
#endregion

namespace SimpleCodeGenerator.Generators
{
	/// <summary>
	/// 类型转换类,负责从pdm中的类型转换为c#类型
	/// </summary>
	public static class TypeConverter
	{
		/// <summary>
		/// 负责将PDM中的类型字符串转换为c sharp的类型字符串
		/// </summary>
		/// <param name="pdmType">pdm中的字符串</param>
		/// <returns>转换后的c sharp的类型字符串</returns>
		public static string ConvertFromPDMTypeToCSharpType(string pdmType)
		{
            if (string.Compare(pdmType, "int", true) == 0 
                || string.Compare(pdmType, "smallint", true)==0
               )
				return "int";

            if (string.Compare(pdmType, "tinyint", true) == 0)
				return "short";

			if (string.Compare(pdmType, "bigint", true) == 0)
				return "long";

			if (string.Compare(pdmType, "datetime", true) == 0)
				return "DateTime";

			if (string.Compare(pdmType, "bit", true) == 0)
				return "bool";

			//varchar,nvarchar,text均映射为字符串
			Match match = Regex.Match(pdmType, @"^n?varchar\(.+\)|n?text$");
			if (match.Success)
			{
				return "string";
			}
            throw new Exception("unknown type in PDM:" + pdmType);
		}
	} 
}
